import Vue from 'vue'
import Router from 'vue-router'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css'
import {store} from '../store'

Vue.use(Router)

const router = new Router({
  routes: [

    {
      path: '/dm/login',
      name: '登陆',
      meta: {
        title: '登陆'
      },
      component: () => import('../view/login'),
      children: [
        {
          path: '/',
          name: '登陆',
          meta: {
            title: '登陆'
          },
          component: () => import('../view/login/login')
        },
        {
          path: '/dm/register',
          name: '注册',
          meta: {
            title: '注册'
          },
          component: () => import('../view/login/register')
        },
      ]
    },

    {
      path: '/',
      name: '文档管理',
      meta: {
        title: '文档管理',
        requireAuth: true // 是否需要登陆访问
      },
      component: () => import('../view/index')
    },
    {
      path: '/dm/index',
      name: '文档管理',
      meta: {
        title: '文档管理',
        requireAuth: true // 是否需要登陆访问
      },
      component: () => import('../view/index')
    }
  ]
})

router.beforeEach(async (to, from, next) => {
  NProgress.start()
  if (to.meta.requireAuth) {
    if (store.getters.isLogin) {
      next()
    } else {
      next('/dm/login')
    }
  } else if (to.name) {
    next()
  } else {
    next('/dm/login')
  }
  NProgress.done()
})

router.afterEach(() => {
  NProgress.done()
})

export default router
